//
//  BSXPFTableViewCell.m
//  BaiSiBuDeJie
//
//  Created by XPF on 16/8/29.
//  Copyright © 2016年 tsaievan. All rights reserved.
//

#import "BSXPFTableViewCell.h"
#import "BSXPFIconButton.h"
#import "BSXPFCommentLabel.h"

//BSXPFIconButton *iconBtn = [[BSXPFIconButton alloc] init];
//[self.contentView addSubview:iconBtn];
//
//UIButton *attitudeBtn = [[UIButton alloc] init];
//[self.contentView addSubview:attitudeBtn];
//UILabel *textLab = [[UILabel alloc] init];
//[self.contentView addSubview:textLab];
//UIImageView *contentView = [[UIImageView alloc] init];
//[self.contentView addSubview:contentView];
//BSXPFCommentLabel *commentLab = [[BSXPFCommentLabel alloc] init];
//[self.contentView addSubview:commentLab];
//UIView *sortView = [[UIView alloc] init];
//[self.contentView addSubview:sortView];
//UIView *supportView = [[UIView alloc] init];
//[self.contentView addSubview:supportView];

@interface BSXPFTableViewCell ()

@property (weak,nonatomic) BSXPFIconButton *iconBtn;

@property (weak,nonatomic) UIButton *attitudeBtn;

@property (weak,nonatomic) UILabel *textLab;

@property (weak,nonatomic) UIImageView *myContentView;

@property (weak,nonatomic) BSXPFCommentLabel *commentLab;

@property (weak,nonatomic) UIView *sortView;

@property (weak,nonatomic) UIView *supportView;

@end


@implementation BSXPFTableViewCell


#pragma mark - 初始化cell上的控件

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setUpUI];
        
    }
    
    [self setNeedsUpdateConstraints];
    
    return self;
    
}

- (void)setUpUI{
    
    BSXPFIconButton *iconBtn = [[BSXPFIconButton alloc] init];
    [self.contentView addSubview:iconBtn];
    self.iconBtn = iconBtn;
    
    UIButton *attitudeBtn = [[UIButton alloc] init];
    [self.contentView addSubview:attitudeBtn];
    self.attitudeBtn = attitudeBtn;
    self.attitudeBtn.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0  blue:arc4random_uniform(256)/255.0 alpha:1];
    
    
    UILabel *textLab = [[UILabel alloc] init];
    [self.contentView addSubview:textLab];
    self.textLab = textLab;
    self.textLab.numberOfLines = 0;
    
    UIImageView *contentView = [[UIImageView alloc] init];
    [self.contentView addSubview:contentView];
    self.myContentView = contentView;
    self.myContentView.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0  blue:arc4random_uniform(256)/255.0 alpha:1];
    
    BSXPFCommentLabel *commentLab = [[BSXPFCommentLabel alloc] init];
    [self.contentView addSubview:commentLab];
    self.commentLab = commentLab;
    self.commentLab.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0  blue:arc4random_uniform(256)/255.0 alpha:1];
    
    UIView *sortView = [[UIView alloc] init];
    [self.contentView addSubview:sortView];
    self.sortView = sortView;
    self.sortView.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0  blue:arc4random_uniform(256)/255.0 alpha:1];
    
    UIView *supportView = [[UIView alloc] init];
    [self.contentView addSubview:supportView];
    self.supportView = supportView;
    UIButton *upBtn = [[UIButton alloc]init];
    UIButton *downBtn = [[UIButton alloc] init];
    
    
}

#pragma mark - 给cell上的控件设置布局

-(void)updateConstraints{
    
    [self customLayoutSubviews];
    
    [super updateConstraints];
    
}

- (void)customLayoutSubviews{
    
    __weak __typeof(self)weakSelf = self;
    
    [self.iconBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(weakSelf.contentView).offset(8);
        
        make.left.equalTo(weakSelf.contentView).offset(10);
        
        make.height.offset(40);
       
        make.width.offset(220);
        
    }];
    
    [self.textLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(weakSelf.iconBtn.mas_bottom).offset(12);
        
        make.left.equalTo(weakSelf.contentView).offset(10);
        
        make.right.equalTo(weakSelf.contentView).offset(-20);
        
        
    }];
    
    [self.myContentView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(weakSelf.textLab.mas_bottom).offset(10);
        
        make.left.equalTo(weakSelf.contentView).offset(8);
        
        make.right.equalTo(weakSelf.contentView).offset(-8);
        
    }];
    
    [self.sortView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(weakSelf.myContentView.mas_bottom).offset(10);
        
        make.left.equalTo(weakSelf.contentView).offset(8);
        
        make.height.offset(40);
        
        make.width.offset(100);
        
    }];
    
    [self.supportView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(weakSelf.sortView.mas_bottom).offset(0);
        
        make.right.left.equalTo(weakSelf.contentView).offset(0);
        
        make.height.offset(40);
        
        make.bottom.equalTo(weakSelf.contentView).offset(0);
        
    }];

}


#pragma mark - 重写model的set方法



-(void)setModel:(ListModel *)model{
    
    _model = model;

    // 设置头像icon
    self.iconBtn.header = model.u.header[0];
    self.iconBtn.name = model.u.name;
    self.iconBtn.passtime = model.passtime;
    
    //设置text文本
    self.textLab.text = model.text;
    
    //  设置图片
  __block  CGFloat height = (CGFloat) model.video.height/model.video.width * (kScreenW - 16);

    __block CGFloat width = (CGFloat)( kScreenW - 30 )/model.video.height * model.video.width;

    __weak typeof(self)weakSelf = self;
    
    [self.myContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.textLab.mas_bottom).offset(10);
        
        if (height > kScreenW - 30) {
            
             make.top.equalTo(weakSelf.textLab.mas_bottom).offset(10);
            
            make.height.offset(kScreenW- 30);
            
            make.width.offset(width);
            
             make.centerX.equalTo(weakSelf.contentView.mas_centerX);
            
        }else{
             make.top.equalTo(weakSelf.textLab.mas_bottom).offset(10);
            
             make.left.equalTo(weakSelf.contentView).offset(8);
            
             make.height.offset(height);
            
             make.right.equalTo(weakSelf.contentView).offset(-8);
            
        }
        
    }];
    NSURL *url = [NSURL URLWithString:model.video.thumbnail_small[0]];
    [self.myContentView sd_setImageWithURL:url];
    
    
}

@end
